home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: presby.edu!jtbell
- From: jtbell@presby.edu (Jon Bell)
- Subject: Re: What is referencing good for?
- Message-ID: <Dnq47s.CBv@presby.edu>
- Date: Mon, 4 Mar 1996 03:26:16 GMT
- References: <4h6ghr$70a@chaos.kulnet.kuleuven.ac.be>
- Organization: Presbyterian College, Clinton, South Carolina USA
-
- Andreas De Troy <Andreas.DeTroy@ped.kuleuven.ac.be> wrote:
- >I think the ONLY reason why this referencing-thing is introduced in C++,
- >is to allow operator overloading.
-
- You're basically correct here, although "only" is a bit too strong. In
- his book "The Design and Evolution of C++", Bjarne Stroustrup writes:
-
- >References were introduced primarily to support operator overloading.
- >[...]
- >
- >C passes every function argument by value, and where passing an object
- >by value would be inefficient or inappropriate the user can pass a
- >pointer. This strategy doesn't work where operator overloading is used.
- >In that case, notational convenience is essential because users cannot be
- >expected to insert address-of operators if the objects are large. For
- >example:
- >
- > a = b - c;
- >
- >is acceptable (that is, conventional) notation, but
- >
- > a = &b - &c;
- >
- >is not. Anyway, &b-&c already has a meaning in C, and I didn't want to
- >change that.
-
- You write:
-
- >For the rest referencing is a rather dangerous thing and I would never
- >recommend using it. People who say that it allows "cleaner" code are just
- >missing the point. In "C" if you see something like:
- >
- > int a, b, c, d;
- >
- > swap (&a, &b);
- > perform_thing (c, d);
- >
- >.. you know that a and b can be changed inside the function, and c and d
- >not. This is very helpful for debugging. In C++ you don't know this, and
- >you actually have to look at the exact function-definition, somewhere in
- >a header, to make sure that c and d can be modified or not.
- >
- >So I would NEVER use the &-operator to produce so-called "cleaner" code.
-
- This is a matter of opinion. Having programmed in Fortran and Pascal for
- years already, I found that my most common errors in writing C programs
- were (a) forgetting to insert the '&' after an argument when calling a
- function which passes a pointer, and (b) forgetting to insert the '*' when
- using a pointer argument inside a function, when I wanted the thing the
- pointer pointed to. I am very grateful that C++ allows me to avoid
- stupid little mistakes like that by using references instead. (I should
- note that I very seldom need to do pointer arithmetic.)
-
- But I don't begrudge you your opinion. C++ has the flexibility to allow
- us both to follow our preferences. I just hope I never have to modify
- any of your code, because I will surely forget those blasted '*'s again!
- :-) :-)
-
- --
- Jon Bell <jtbell@presby.edu> Presbyterian College
- Dept. of Physics and Computer Science Clinton, South Carolina USA
-